home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 15 / CU Amiga Magazine's Super CD-ROM 15 (1997)(EMAP Images)(GB)[!][issue 1997-10].iso / CUCD / Graphics / Ghostscript / source / zmedia2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-16  |  12.4 KB  |  445 lines

  1. /* Copyright (C) 1993, 1995, 1996, 1997 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* zmedia.c */
  20. /* Media matching for setpagedevice */
  21. #include "math_.h"
  22. #include "memory_.h"
  23. #include "ghost.h"
  24. #include "gsmatrix.h"
  25. #include "errors.h"
  26. #include "oper.h"
  27. #include "idict.h"
  28. #include "idparam.h"
  29. #include "iname.h"
  30. #include "store.h"
  31.  
  32. /* <pagedict> <attrdict> <policydict> <keys> .matchmedia <key> true */
  33. /* <pagedict> <attrdict> <policydict> <keys> .matchmedia false */
  34. /* <pagedict> null <policydict> <keys> .matchmedia null true */
  35. private int zmatch_page_size(P8(const ref *pvreq, const ref *pvmed,
  36.   int policy, int orient, bool roll, float *best_mismatch, gs_matrix *pmat,
  37.   gs_point *pmsize));
  38. private int
  39. zmatchmedia(register os_ptr op)
  40. {    os_ptr preq = op - 3;
  41.     os_ptr pattr = op - 2;
  42.     os_ptr ppol = op - 1;
  43. #define pkeys op
  44.     int policy_default;
  45.     float best_mismatch = (float)max_long;        /* adhoc */
  46.     ref mmkey, nmkey;
  47.     float mbest = best_mismatch;
  48.     uint matched_priority;
  49.     ref no_priority;
  50.     ref *ppriority;
  51.     int mepos, orient;
  52.     bool roll;
  53.     int code;
  54.     int ai;
  55.     ref aelt[2];
  56. #define mkey aelt[0]
  57. #define mdict aelt[1]
  58.  
  59.     if ( r_has_type(pattr, t_null) )
  60.       {    check_op(4);
  61.         make_null(op - 3);
  62.         make_true(op - 2);
  63.         pop(2);
  64.         return 0;
  65.       }
  66.     check_type(*preq, t_dictionary);
  67.     check_dict_read(*preq);
  68.     check_type(*pattr, t_dictionary);
  69.     check_dict_read(*pattr);
  70.     check_type(*ppol, t_dictionary);
  71.     check_dict_read(*ppol);
  72.     check_array(*pkeys);
  73.     check_read(*pkeys);
  74.     switch ( code = dict_int_null_param(preq, "MediaPosition", 0, 0x7fff,
  75.                         0, &mepos) )
  76.       {
  77.       default: return code;
  78.       case 2:
  79.       case 1: mepos = -1;
  80.       case 0: ;
  81.       }
  82.     switch ( code = dict_int_null_param(preq, "Orientation", 0, 3,
  83.                         0, &orient) )
  84.       {
  85.       default: return code;
  86.       case 2:
  87.       case 1: orient = -1;
  88.       case 0: ;
  89.       }
  90.     code = dict_bool_param(preq, "RollFedMedia", false, &roll);
  91.     if ( code < 0 )
  92.       return code;
  93.     code = dict_int_param(ppol, "PolicyNotFound", 0, 7, 0,
  94.                   &policy_default);
  95.     if ( code < 0 )
  96.       return code;
  97.     if ( dict_find_string(pattr, "Priority", &ppriority) > 0 )
  98.     {    check_array_only(*ppriority);
  99.         check_read(*ppriority);
  100.     }
  101.     else
  102.     {    make_empty_array(&no_priority, a_readonly);
  103.         ppriority = &no_priority;
  104.     }
  105. #define reset_match()\
  106.   matched_priority = r_size(ppriority),\
  107.   make_null(&mmkey),\
  108.   make_null(&nmkey)
  109.     reset_match();
  110.     for ( ai = dict_first(pattr); (ai = dict_next(pattr, ai, aelt)) >= 0; )
  111.     {    if ( r_has_type(&mdict, t_dictionary) &&
  112.              r_has_attr(dict_access_ref(&mdict), a_read) &&
  113.              r_has_type(&mkey, t_integer) &&
  114.              (mepos < 0 || mkey.value.intval == mepos)
  115.            )
  116.         {    bool match_all;
  117.             uint ki, pi;
  118.  
  119.             code = dict_bool_param(&mdict, "MatchAll", false,
  120.                            &match_all);
  121.             if ( code < 0 )
  122.               return code;
  123.             for ( ki = 0; ki < r_size(pkeys); ki++ )
  124.             {    ref key;
  125.                 ref kstr;
  126.                 ref *prvalue;
  127.                 ref *pmvalue;
  128.                 ref *ppvalue;
  129.                 int policy;
  130.  
  131.                 array_get(pkeys, ki, &key);
  132.                 if ( dict_find(&mdict, &key, &pmvalue) <= 0 )
  133.                   continue;
  134.                 if ( dict_find(preq, &key, &prvalue) <= 0 ||
  135.                      r_has_type(prvalue, t_null)
  136.                    )
  137.                 {    if ( match_all )
  138.                       goto no;
  139.                     else
  140.                       continue;
  141.                 }
  142.                 /* Look for the Policies entry for this key. */
  143.                 if ( dict_find(ppol, &key, &ppvalue) > 0 )
  144.                   { check_type_only(*ppvalue, t_integer);
  145.                     policy = ppvalue->value.intval;
  146.                   }
  147.                 else
  148.                   policy = policy_default;
  149.     /*
  150.      * Match a requested attribute value with the attribute value in the
  151.      * description of a medium.  For all attributes except PageSize,
  152.      * matching means equality.  PageSize is special; see match_page_size
  153.      * below.
  154.      */
  155.                 if ( r_has_type(&key, t_name) &&
  156.                      (name_string_ref(&key, &kstr),
  157.                      r_size(&kstr) == 8 &&
  158.                      !memcmp(kstr.value.bytes, "PageSize", 8))
  159.                    )
  160.                   { gs_matrix ignore_mat;
  161.                     gs_point ignore_msize;
  162.                     if ( zmatch_page_size(prvalue, pmvalue,
  163.                               policy, orient, roll,
  164.                               &best_mismatch,
  165.                               &ignore_mat,
  166.                               &ignore_msize)
  167.                     <= 0 )
  168.                       goto no;
  169.                   }
  170.                 else
  171.                   if ( !obj_eq(prvalue, pmvalue) )
  172.                     goto no;
  173.             }
  174.             /* We have a match.  If it is a better match */
  175.             /* than the current best one, it supersedes it */
  176.             /* regardless of priority. */
  177.             if ( best_mismatch < mbest )
  178.               { mbest = best_mismatch;
  179.                 reset_match();
  180.               }
  181.             /* In case of a tie, see if the new match has */
  182.             /* priority. */
  183.             for ( pi = matched_priority; pi > 0; )
  184.             {    ref pri;
  185.                 pi--;
  186.                 array_get(ppriority, pi, &pri);
  187.                 if ( obj_eq(&mkey, &pri) )
  188.                 {    /* Yes, higher priority. */
  189.                     mmkey = mkey;
  190.                     matched_priority = pi;
  191.                     break;
  192.                 }
  193.             }
  194.             /* Save the match in case no match has priority. */
  195.             nmkey = mkey;
  196. no:            ;
  197.         }
  198.     }
  199. #undef mkey
  200. #undef mdict
  201. #undef pkeys
  202.     if ( r_has_type(&nmkey, t_null) )
  203.     {    make_false(op - 3);
  204.         pop(3);
  205.     }
  206.     else
  207.     {    if ( r_has_type(&mmkey, t_null) )
  208.           op[-3] = nmkey;
  209.         else
  210.           op[-3] = mmkey;
  211.         make_true(op - 2);
  212.         pop(2);
  213.     }
  214.     return 0;
  215. }
  216.  
  217. /* [<req_x> <req_y>] [<med_x0> <med_y0> (<med_x1> <med_y1> | )]
  218.  *     <policy> <orient|null> <roll> <matrix|null> .matchpagesize
  219.  *   <matrix|null> <med_x> <med_y> true   -or-  false
  220.  */
  221. private int
  222. zmatchpagesize(register os_ptr op)
  223. {    gs_matrix mat;
  224.     float ignore_mismatch = (float)max_long;
  225.     gs_point media_size;
  226.     int orient;
  227.     bool roll;
  228.     int code;
  229.  
  230.     check_type(op[-3], t_integer);
  231.     if ( r_has_type(op - 2, t_null) )
  232.       orient = -1;
  233.     else
  234.       { check_int_leu(op[-2], 3);
  235.         orient = (int)op[-2].value.intval;
  236.       }
  237.     check_type(op[-1], t_boolean);
  238.     roll = op[-1].value.boolval;
  239.     code = zmatch_page_size(op - 5, op - 4, (int)op[-3].value.intval,
  240.                 orient, roll,
  241.                 &ignore_mismatch, &mat, &media_size);
  242.     switch ( code )
  243.       {
  244.       default:
  245.         return code;
  246.       case 0:
  247.         make_false(op - 5);
  248.         pop(5);
  249.         break;
  250.       case 1:
  251.         code = write_matrix(op, &mat);
  252.         if ( code < 0 && !r_has_type(op, t_null) )
  253.           return code;
  254.         op[-5] = *op;
  255.         make_real(op - 4, media_size.x);
  256.         make_real(op - 3, media_size.y);
  257.         make_true(op - 2);
  258.         pop(2);
  259.         break;
  260.       }
  261.     return 0;
  262. }
  263. /* Match the PageSize.  See below for details. */
  264. private bool match_page_size(P8(const gs_point *request,
  265.   const gs_rect *medium, int policy, int orient, bool roll,
  266.   float *best_mismatch, gs_matrix *pmat, gs_point *pmsize));
  267. private int
  268. zmatch_page_size(const ref *pvreq, const ref *pvmed, int policy, int orient,
  269.   bool roll, float *best_mismatch, gs_matrix *pmat, gs_point *pmsize)
  270. {    uint nm;
  271.     check_array(*pvreq);
  272.     check_array(*pvmed);
  273.     if ( !(r_size(pvreq) == 2 && ((nm = r_size(pvmed)) == 2 || nm == 4)) )
  274.       return_error(e_rangecheck);
  275.     {    ref rv[6];
  276.         uint i;
  277.         float v[6];
  278.         int code;
  279.  
  280.         array_get(pvreq, 0, &rv[0]);
  281.         array_get(pvreq, 1, &rv[1]);
  282.         for ( i = 0; i < 4; ++i )
  283.           array_get(pvmed, i % nm, &rv[i + 2]);
  284.         if ( (code = num_params(rv + 5, 6, v)) < 0 )
  285.           return code;
  286.         { gs_point request;
  287.           gs_rect medium;
  288.  
  289.           request.x = v[0], request.y = v[1];
  290.           medium.p.x = v[2], medium.p.y = v[3],
  291.             medium.q.x = v[4], medium.q.y = v[5];
  292.           return match_page_size(&request, &medium, policy, orient,
  293.                      roll, best_mismatch, pmat, pmsize);
  294.         }
  295.     }
  296. }
  297. /*
  298.  * Match a requested PageSize with the PageSize of a medium.  The medium
  299.  * may specify either a single value [mx my] or a range
  300.  * [mxmin mymin mxmax mymax]; matching means equality or inclusion
  301.  * to within a tolerance of 5, possibly swapping the requested X and Y.
  302.  * Take the Policies value into account, keeping track of the discrepancy
  303.  * if needed.  When a match is found, also return the matrix to be
  304.  * concatenated after setting up the default matrix, and the actual
  305.  * media size.
  306.  *
  307.  * NOTE: The algorithm here doesn't work properly for variable-size media
  308.  * when the match isn't exact.  We'll fix it if we ever need to.
  309.  */
  310. private void make_adjustment_matrix(P5(const gs_point *request,
  311.   const gs_rect *medium, gs_matrix *pmat, bool scale, int rotate));
  312. private bool
  313. match_page_size(const gs_point *request, const gs_rect *medium, int policy,
  314.   int orient, bool roll, float *best_mismatch, gs_matrix *pmat,
  315.   gs_point *pmsize)
  316. {    double rx = request->x, ry = request->y;
  317.  
  318.     if ( policy == 7 )
  319.       {        /* (Adobe) hack: just impose requested values */
  320.         *best_mismatch = 0;
  321.         gs_make_identity(pmat);
  322.         *pmsize = *request;
  323.         return true;
  324.       }
  325.     if ( rx - medium->p.x >= -5 && rx - medium->q.x <= 5 &&
  326.          ry - medium->p.y >= -5 && ry - medium->q.y <= 5 &&
  327.          (orient < 0 || !(orient & 1))
  328.        )
  329.       { *best_mismatch = 0;
  330.         make_adjustment_matrix(request, medium, pmat, false,
  331.                    (orient >= 0 ? orient : 0));
  332.       }
  333.     else if ( rx - medium->p.y >= -5 && rx - medium->q.y <= 5 &&
  334.          ry - medium->p.x >= -5 && ry - medium->q.x <= 5 &&
  335.          (orient < 0 || (orient & 1))
  336.        )
  337.       { *best_mismatch = 0;
  338.         make_adjustment_matrix(request, medium, pmat, false,
  339.                    (orient >= 0 ? orient :
  340.                     rx < ry ? -1 : 1));
  341.       }
  342.     else
  343.     { int rotate =
  344.         (orient >= 0 ? orient :
  345.          rx < ry ?
  346.          (medium->q.x > medium->q.y ? -1 : 0) :
  347.          (medium->q.x < medium->q.y ? 1 : 0));
  348.       bool larger =
  349.         (rotate ? medium->q.y >= rx && medium->q.x >= ry :
  350.          medium->q.x >= rx && medium->q.y >= ry);
  351.       bool adjust = false;
  352.       float mismatch = medium->q.x * medium->q.y - rx * ry;
  353.  
  354.       switch ( policy )
  355.         {
  356.         default:        /* exact match only */
  357.           return false;
  358.         case 3:        /* nearest match, adjust */
  359.           adjust = true;
  360.         case 5:        /* nearest match, don't adjust */
  361.           if ( fabs(mismatch) >= fabs(*best_mismatch) )
  362.         return false;
  363.           break;
  364.         case 4:        /* next larger match, adjust */
  365.           adjust = true;
  366.         case 6:        /* next larger match, don't adjust */
  367.           if ( !larger || mismatch >= *best_mismatch )
  368.         return false;
  369.           break;
  370.         }
  371.       if ( adjust )
  372.         make_adjustment_matrix(request, medium, pmat, !larger, rotate);
  373.       else
  374.         { gs_rect req_rect;
  375.           req_rect.p = *request;
  376.           req_rect.q = *request;
  377.           make_adjustment_matrix(request, &req_rect, pmat, false, rotate);
  378.         }
  379.       *best_mismatch = mismatch;
  380.     }
  381.     if ( pmat->xx == 0 )
  382.       { /* Swap request X and Y. */
  383.         double temp = rx;
  384.         rx = ry, ry = temp;
  385.       }
  386. #define adjust_into(req, mmin, mmax)\
  387.   (req < mmin ? mmin : req > mmax ? mmax : req)
  388.     pmsize->x = adjust_into(rx, medium->p.x, medium->q.x);
  389.     pmsize->y = adjust_into(ry, medium->p.y, medium->q.y);
  390.     return true;
  391. }
  392. /*
  393.  * Compute the adjustment matrix for scaling and/or rotating the page
  394.  * to match the medium.  If the medium is completely flexible in a given
  395.  * dimension (e.g., roll media in one dimension, or displays in both),
  396.  * we must adjust its size in that dimension to match the request.
  397.  * We recognize this by medium->p.{x,y} = 0.
  398.  */
  399. private void
  400. make_adjustment_matrix(const gs_point *request, const gs_rect *medium,
  401.   gs_matrix *pmat, bool scale, int rotate)
  402. {    double rx = request->x, ry = request->y;
  403.     double mx = medium->q.x, my = medium->q.y;
  404.  
  405.     /* Rotate the request if necessary. */
  406.     if ( rotate & 1 )
  407.       { double temp = rx;
  408.         rx = ry, ry = temp;
  409.       }
  410.  
  411.     /* Adjust the medium size if flexible. */
  412.     if ( medium->p.x == 0 && mx > rx )
  413.       mx = rx;
  414.     if ( medium->p.y == 0 && my > ry )
  415.       my = ry;
  416.  
  417.     /* Translate to align the centers. */
  418.     gs_make_translation(mx / 2, my / 2, pmat);
  419.  
  420.     /* Rotate if needed. */
  421.     if ( rotate )
  422.       gs_matrix_rotate(pmat, 90.0 * rotate, pmat);
  423.  
  424.     /* Scale if needed. */
  425.     if ( scale )
  426.       { double xfactor = mx / rx;
  427.         double yfactor = my / ry;
  428.         double factor = min(xfactor, yfactor);
  429.         if ( factor < 1 )
  430.           gs_matrix_scale(pmat, factor, factor, pmat);
  431.       }
  432.  
  433.     /* Now translate the origin back, */
  434.     /* using the original, unswapped request. */
  435.     gs_matrix_translate(pmat, -request->x / 2, -request->y / 2, pmat);
  436. }
  437.  
  438. /* ------ Initialization procedure ------ */
  439.  
  440. BEGIN_OP_DEFS(zmedia2_l2_op_defs) {
  441.         op_def_begin_level2(),
  442.     {"4.matchmedia", zmatchmedia},
  443.     {"6.matchpagesize", zmatchpagesize},
  444. END_OP_DEFS(0) }
  445.